import { RouteObject } from "./type";
import JSEncrypt from 'jsencrypt';
/**
 * @description 递归查询对应的路由
 * @param {String} path 当前访问地址
 * @param {Array} routes 路由列表
 * @returns array
 */
export const searchRoute = (path: string, routes: RouteObject[] = []): RouteObject => {
    let result: RouteObject = {};
    for (let item of routes) {
        if (item.path === path) return item;
        if (item.children) {
            const res = searchRoute(path, item.children);
            if (Object.keys(res).length) result = res;
        }
    }
    return result;
};

/**
 * @description 获取需要展开的 subMenu
 * @param {String} path 当前访问地址
 * @returns array
 */
export const getOpenKeys = (path: string) => {
    let newStr: string = "";
    let newArr: any[] = [];
    let arr = path.split("/").map(i => "/" + i);
    for (let i = 1; i < arr.length - 1; i++) {
        newStr += arr[i];
        newArr.push(newStr);
    }
    return newArr;
};

/**
 * @description 加密函数
 * @param {String} pwd 密码字段
 * @returns string
 */
export const encrypt = (pwd: string) => {
    let key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQCRRX24I2Z2Sp3aVE8uGVmRWqw0pT4jXpWhdSCd9RpfBQXj054iTY5WfZtLhztbGyJGhxXIVTx4/JR3hZsMtMONIXz2gHd2ZFBQ2WA/6ZZp3RQ4tC8nCMx0j8j0hIgo1Yw55OW9bvRtnqbKdEWAHDjbmF321s7IiEaUnU7yheAQIDAQAB"
    let encrypt = new JSEncrypt();
    encrypt.setPublicKey(key);
    let encrypted = encrypt.encrypt(pwd);
    return encrypted;
}




